
Imports std
Imports System.IO
Imports common

Module main

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim reader As TextReader = Nothing
        Dim writer As TextWriter = Nothing
        Dim input As String = Nothing
        Dim kicks As String = Nothing
        Dim i As Int32

        If args.Length() < 3 Then
            raiseError("argument count error, input1 input2 ... inputN output kickChars, " + _
                        "if input or output is /-, use console, backslash supported.", errorHandle.errorType.critical)
        Else
            copy(kicks, args(args.Length() - 1))
            unescape(kicks)
            If args(args.Length() - 2) = "/-" Then
                writer = Console.Out
            Else
                writer = New StreamWriter(New FileStream(args(args.Length() - 2), _
                                            FileMode.Create, FileAccess.Write, FileShare.Read))
            End If
            assert(Not writer Is Nothing, "file no found " + args(args.Length() - 2))

            For i = 0 To args.Length() - 3
                If args(i) = "/-" Then
                    reader = Console.In
                Else
                    reader = New StreamReader(New FileStream(args(i), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                End If
                assert(Not reader Is Nothing, "file no found " + args(i))

                input = reader.ReadLine
                While Not input Is Nothing
                    stringUtils.kickChars(input, kicks)
                    writer.WriteLine(input)
                    input = reader.ReadLine
                End While

                closestream(reader)
            Next

            closestream(writer)
        End If
    End Sub

End Module
